﻿using Serialization.Script;
using Microsoft.Xna.Framework;
using GlobalGameClasses;
using System.Linq;
using System;
using DrawableObjects.Actors.Weapons;
using System.Diagnostics;

namespace DrawableObjects.Actors.Aircrafts.Controller
{
    /// <summary>
    /// A controller for an endboss in the game. Is used to order an aircraft to follow predefined moving patterns 
    /// and fire their weapons in a certain order
    /// </summary>
    public class AIControlledAircraft_Boss : AircraftController
    {
        private enum BossMovement {MOVE_LEFT, MOVE_MIDDLE, MOVE_RIGHT, STOP};
        private enum BossAttack { ATTACK_MISSILE, ATTACK_LIGHTNING, ATTACK_LASER, ATTACK_SONAR, STOP };

        //Relative vector from upper left corner of the aircrafts bounding to predefined point within the bounding
        public Vector2 directionMiddle; 
        public Vector2 directionLeft;
        public Vector2 directionRight;

        private BossMovement currentMovement;
        private BossMovement lastMovement;
        private BossAttack currentAttack;

        //Length of one movementPhase in Ms
        private float phaseLength = 5000;
        //Period until next phase in Ms
        private float counter = 0;

        private Vector2 currentDestination;

        //Time until final attack is triggered
        private float countdown = 60000;

        public override void Update()
        {
            //Movement-section: Executes the aicrafts movements
            counter -= (float)GlobalGameValues.Values.gameTime.ElapsedGameTime.TotalMilliseconds;
                currentDestination = aircraft.worldBounding.upperLeft;
                if (currentMovement == BossMovement.MOVE_RIGHT)
                {
                    currentDestination += directionRight;
                }
                else if (currentMovement == BossMovement.MOVE_MIDDLE)
                {
                    currentDestination += directionMiddle;
                }
                else if (currentMovement == BossMovement.MOVE_LEFT)
                {
                    currentDestination += directionLeft;
                }
                if (currentMovement != BossMovement.STOP)
                {
                    aircraft.moveTowards(currentDestination);
                }
            if (counter <= 0)
            {
                setModes();
            }
            else if (currentMovement != BossMovement.STOP && aircraft.boundingSphere.Contains(new Vector3(currentDestination.X, GlobalGameValues.GAMEFIELD_HEIGHT, currentDestination.Y)) == ContainmentType.Contains)
            {
                setModes();
            }

            // Fire-section: Executes the aircrafts attacks
            if (countdown < 0)
            {
                //Triggers final attack
                currentMovement = BossMovement.STOP;
                int i = 0;
                foreach (Weaponsystem system in aircraft.weaponsystems)
                {
                    if (system.weaponCategory == Weapon.WeaponType.Sonarwave && system.IsReady())
                    {
                        this.aircraft.Fire(i, new Vector2(0, -1));
                    }
                    i++;
                }

            }
            else if (currentAttack == BossAttack.ATTACK_LIGHTNING)
            {
                Actor target = null;
                int i = 0;
                foreach (Weaponsystem system in aircraft.weaponsystems)
                {
                    if (system.weaponCategory == Weapon.WeaponType.Lightning && system.IsReady())
                    {
                        //targets the player and fires lightning ball
                        if (target == null) target = aircraft.world.GetClosestElement(aircraft.getGamePosition(), typeof(Aircraft), Owner.PLAYER);
                        if (target != null) this.aircraft.Fire(i, Vector2.Subtract(target.getGamePosition(), aircraft.getGamePosition()));
                    }
                    i++;
                }
            }
            else if (currentAttack == BossAttack.ATTACK_MISSILE)
            {
                int i = 0;
                foreach (Weaponsystem system in aircraft.weaponsystems)
                {
                    if (system.weaponCategory == Weapon.WeaponType.Missile && system.IsReady())
                    {
                        this.aircraft.Fire(i, new Vector2(0, 1));
                    }
                    i++;
                }
            }
            else if (currentAttack == BossAttack.ATTACK_LASER)
            {
                int i = 0;
                foreach (Weaponsystem system in aircraft.weaponsystems)
                {
                    if (system.weaponCategory == Weapon.WeaponType.Laser && system.IsReady())
                    {
                        this.aircraft.Fire(i, new Vector2(0, 1));
                    }
                    i++;
                }
            }
            countdown -= (float)GlobalGameClasses.GlobalGameValues.Values.gameTime.ElapsedGameTime.TotalMilliseconds;
        }


        /// <summary>
        /// Sets movement and attack modes depending on the last active movement modes
        /// </summary>
        private void setModes()
        {
            if (currentMovement == BossMovement.STOP)
            {
                //  if boss stopped, move him again

                currentAttack = BossAttack.ATTACK_LASER;
                if (lastMovement == BossMovement.MOVE_LEFT || lastMovement == BossMovement.MOVE_RIGHT)
                {
                    lastMovement = currentMovement;
                    currentMovement = BossMovement.MOVE_MIDDLE;
                }
                else
                {

                    lastMovement = currentMovement;
                    Random rnd = new Random();
                    int move = rnd.Next(0, 2);
                    if (move == 0)
                    {
                        currentMovement = BossMovement.MOVE_RIGHT;
                    }
                    else
                    {
                        currentMovement = BossMovement.MOVE_LEFT;
                    }
                }
            }
            else
            {
                // Depending on last movement, trigger attacks
                if (currentMovement == BossMovement.MOVE_MIDDLE)
                {
                    currentAttack = BossAttack.ATTACK_LIGHTNING;
                }
                else
                {
                    currentAttack = BossAttack.ATTACK_MISSILE;
                }
                // If Actor was moving, stop 
                lastMovement = currentMovement;
                currentMovement = BossMovement.STOP;
            }
            counter = phaseLength;
        }

        public AIControlledAircraft_Boss(Aircraft Aircraft)
        {
            this.counter = this.phaseLength;
            this.aircraft = Aircraft;
            this.currentMovement = BossMovement.MOVE_MIDDLE;
            this.lastMovement = BossMovement.STOP;
        }
    }
}

